package com.gagakuai.spring.mapper;

import com.gagakuai.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.FactoryBean;

/*
 * @program: common-starters
 * @description: MapperBean
 * @author: houhong
 * @create: 2023-07-15 23:15
 */
public class MapperFactoryBean<T>  extends SqlSessionDaoSupport implements FactoryBean<T> {

    private Class<T> mapperInterface;


    private boolean addToConfig = true;

    public MapperFactoryBean() {
        //intentionally empty
    }

    public MapperFactoryBean(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Override
    public T getObject() throws Exception {
        return getSqlSession().getMapper(this.mapperInterface);
    }


    /*
     * {@inheritDoc}
     */
    @Override
    public Class<T> getObjectType() {
        return this.mapperInterface;
    }

    /*
     * {@inheritDoc}
     */
    @Override
    public boolean isSingleton() {
        return true;
    }



    public void setMapperInterface(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }


    public Class<T> getMapperInterface() {
        return mapperInterface;
    }

    public void setAddToConfig(boolean addToConfig) {
        this.addToConfig = addToConfig;
    }


    public boolean isAddToConfig() {
        return addToConfig;
    }
}